package com.habook.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;

import javax.swing.JOptionPane;

import org.ho.yaml.Yaml;

public class Property {
	public final static String PROPS_FILE_NAME = "habook.properties";
	private final static Properties props = new Properties();
	static {
		try {
			props.load(new FileInputStream(PROPS_FILE_NAME));
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, String.format("Can't load \"%s\", it should be in application home dir", PROPS_FILE_NAME), //
					"Error Message", JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}
	}

	public final static String MAIL_SERVER_INCOMING = props.getProperty("MAIL_SERVER_INCOMING");
	public final static String MAIL_SERVER_OUTGOING = props.getProperty("MAIL_SERVER_OUTGOING");

	public final static String INCOMING_PORT = props.getProperty("INCOMING_PORT");
	public final static String OUTGOING_PORT = props.getProperty("OUTGOING_PORT");

	public final static String MAIL_SERVER_AUTH = props.getProperty("MAIL_SERVER_AUTH");
	public final static String MAIL_SERVER_USER = props.getProperty("MAIL_SERVER_USER");
	public final static String MAIL_SERVER_PASSWORD = props.getProperty("MAIL_SERVER_PASSWORD");
	public final static String MAIL_DEBUG = props.getProperty("MAIL_DEBUG");
	public final static String MAIL_CONTENT_TYPE = props.getProperty("MAIL_CONTENT_TYPE");

	public final static String INBOX = "INBOX";
	public final static String POP_MAIL = "pop3";
	public final static String SMTP_MAIL = "smtp";

	private final static String USER_HOME = System.getProperty("user.home");
	private final static String HANBOOK_HOME = USER_HOME + File.separator + "Java Handy Book";
	static {
		new File(HANBOOK_HOME).mkdirs();
	}

	private final static String HANBOOK_HANDYNOTES = "HandyNotes";
	private final static String HANBOOK_HANDYNOTES_MAIL = "HandyNotesMail";

	private static String getAbsSavePath(String fname) {
		return HANBOOK_HOME + File.separator + fname + ".yml";
	}

	public static String getAbsLocalNotePath() {
		return getAbsSavePath(HANBOOK_HANDYNOTES);
	}

	public static boolean saveLocalNote(String text) {
		return saveNotepad(text, getAbsSavePath(HANBOOK_HANDYNOTES));
	}

	public static boolean setLastSyscNote(String text) {
		return saveNotepad(text, getAbsSavePath(HANBOOK_HANDYNOTES_MAIL));
	}

	public static String loadLocalNote() {
		return loadNotepad(getAbsSavePath(HANBOOK_HANDYNOTES));
	}

	public static String loadLastSyncNote() { // use cache
		return loadNotepad(getAbsSavePath(HANBOOK_HANDYNOTES_MAIL));
	}

	private static String loadNotepad(String path) {
		try {
			return (String) Yaml.load(new File(path));
		} catch (FileNotFoundException e) {
			return ""; // first time
		}
	}

	private static boolean saveNotepad(String text, String path) {
		File file = new File(path);
		if (file.exists() && file.isDirectory()) // If there is the same name folder
			file.delete(); // delete it
		try {
			Yaml.dump(text, new File(path));
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace(); // Not enough permissions etc.
			return false;
		}
	}
}
